package net.uhuijia.appbase.controller;

import java.util.List;

import net.uhuijia.appbase.redis.IRedisDao;
import net.uhuijia.enums.RedisKey;
import net.uhuijia.enums.UserAuth;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.uhuijia.appbase.model.BaseExample;
import net.uhuijia.appbase.model.BaseModel;
import net.uhuijia.appbase.service.BaseService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class  BaseController<S extends BaseService<M, E>,M extends BaseModel,E extends BaseExample> {
	// 商品过滤字段
	protected static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime",
			"note", "createTime", "orgCode"};
	@Autowired
	protected S s;
	@Autowired
	protected IRedisDao<String,Object> redisDao;

	/**
     * 添加一条记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@RequestBody M m)
	{
    	 return s.add(m);
	}

    /**
     * 根据ID删除一条记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public String del(@PathVariable("id") String id)
	{
    	 M m = s.get(id);
		 if (null == m) {
				throw new RuntimeException("该ID：" + id + "没有没有对应的信息");
		 }
		 return s.delete(m);
	}
    
    
    /**
     * 编辑一条记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(@RequestBody M m)
	{
    	 return s.edit(m);
	}
    
    /**
     * 根据ID查找
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public M get(@PathVariable("id") String id)
	{
    	 M m = s.get(id);
		 if (null == m) {
				throw new RuntimeException("该ID：" + id + "没有没有对应的信息");
		 }
		 return m;
	}
    
    /**
     * 查找数据列表
     * @param example
     * @return
     */
    @RequestMapping(value = "/list")
	public List<M> list( @RequestBody E example)
	{
		return s.list(example);
	}
    
    /**
     * 统计数量
     * @param example
     * @return
     */
    @RequestMapping(value = "/count")
	public Integer count( @RequestBody E example)
	{
		return s.count(example);
	}

	/**
	 * 将授权的用户信息Set到redis里
	 * @param userMall 用户信息
	 * @param token 授权token
	 * @param device 设备号
	 */
	public void setUserMallAuthInfoToRedis(UserMall userMall,String token,String device){
		StringBuffer authKey = new StringBuffer(RedisKey.USER_TOKEN_DEVICE_AUTH.getKey());
		authKey.append(token).append(device);
		this.redisDao.set(authKey.toString(),userMall,7*24*60*60*1L);
	}

	/**
	 * 根据授权的token 和device获取用户信息
	 * @return
	 */
	public UserMall getUserMallByTokenAndDevice(){
		String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
				.getHeader(UserAuth.token.toString());
		String device = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
				.getHeader(UserAuth.device.toString());
		StringBuffer authKey = new StringBuffer(RedisKey.USER_TOKEN_DEVICE_AUTH.getKey());
		authKey.append(token).append(device);
		UserMall userMall =(UserMall) this.redisDao.get(authKey.toString());
		return userMall;
	}

}
